﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Applications.City.Shop;

namespace XingFuStudio.Pioneer.Applications.City.Element {
    /// <summary>
    /// 农业信息实体
    /// </summary>
    public class AgricultureInfo : CityElement{

        /// <summary>
        /// 更种时间
        /// </summary>
        public DateTime PlantTime {
            get;
            set;
        }

        /// <summary>
        /// 获取是否可以收获
        /// </summary>
        /// <param name="farms"></param>
        /// <returns></returns>
        public bool GetCanHarvest(List<ShopItem> farms) {
            var res = farms.Where(s => s.ID == this.ElementId);

            if (res.Count() > 0) {
                FarmShopInfo farm = res.First() as FarmShopInfo;
                if (farm != null && this.PlantTime != DateTime.MinValue) {
                    if (this.PlantTime.AddMinutes(farm.GrowTimes) < DateTime.Now) {
                        return true;
                    }
                }
            } else {
                if (this.PlantTime.AddDays(1) < DateTime.Now) {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 获取是否枯萎
        /// </summary>
        /// <param name="farms"></param>
        /// <returns></returns>
        public bool GetIsWither(List<ShopItem> farms) {
            var res = farms.Where(s => s.ID == this.ElementId);

            if (res.Count() > 0) {
                FarmShopInfo farm = res.First() as FarmShopInfo;
                if (farm != null && this.PlantTime != DateTime.MinValue) {
                    if (this.PlantTime.AddMinutes(farm.GrowTimes * 3) < DateTime.Now) {
                        return true;
                    }
                }
            } else {
                if (this.PlantTime != DateTime.MinValue && this.PlantTime.AddDays(3) < DateTime.Now) {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 获取是否可以播种
        /// </summary>
        public bool CanSow {
            get {
                return this.PlantTime == DateTime.MinValue;
            }
        }

        /// <summary>
        /// 初始化元素自身数据
        /// </summary>
        /// <param name="elementObject"></param>
        public override void InitElement(Dictionary<string, object> elementObject) {
            //调用基类方法
            base.InitElement(elementObject);

            //赋值自身属性
            if ((string)elementObject["planttime"] == "0000-00-00 00:00:00") {
                this.PlantTime = DateTime.MinValue;
            } else {
                this.PlantTime = Convert.ToDateTime((string)elementObject["planttime"]);
            }
        }
    }
}
